//父子进程使用无名管道通信
#include <my_head.h>

int main(int argc, const char *argv[]){
    int fd[2];
    char buff[128];
    if(-1 == pipe(fd))
        ERR_LOG("pipe error");
    pid_t pid = 0;
    if(-1 == (pid = fork())){
        ERR_LOG("fork error");
    }else if(0 < pid ){
        close(fd[0]);//防止误操作，父进程关闭读端
        while(1){
            fgets(buff,sizeof(buff),stdin);
            buff[strlen(buff)-1] = '\0';
            write(fd[1],buff,sizeof(buff));
        }//在终端获取数据，通过管道发给子进程
        close(fd[1]);
    }else if(0 == pid){
        close(fd[1]);//关闭写端，防止误操作
        while(1){
            read(fd[0],buff,sizeof(buff));
            printf("我是子进程 读到管道里面的数据 : [%s]\n",buff);
        }//读取管道中的数据，打印到终端
        close(fd[0]);
    }
    return 0;
}